package com.ifgi.sitcom.campusmapperNDrawer.io;


import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.awt.*;

import org.osmdroid.util.GeoPoint;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Environment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;

import com.gamificationlibrary.model.Area;
import com.gamificationlibrary.model.Ownership;
import com.gamificationlibrary.model.Player;
import com.gamificationlibrary.model.Score;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.Scopes;
import com.google.android.gms.plus.PlusClient;
import com.google.android.gms.plus.model.people.PersonBuffer;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.Syntax;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.sparql.engine.http.QueryEngineHTTP;
import com.hp.hpl.jena.util.FileManager;
import com.ifgi.sitcom.campusmapperNDrawer.activities.MainActivity;
import com.ifgi.sitcom.campusmapperNDrawer.controllers.CampusMapperApplication;
import com.ifgi.sitcom.campusmapperNDrawer.controllers.LeaderboardController;
import com.ifgi.sitcom.campusmapperNDrawer.gamedata.PlayerAdapter;
import com.ifgi.sitcom.campusmapperNDrawer.geometry.*;
import com.ifgi.sitcom.campusmapperNDrawer.indoordata.*;
import com.ifgi.sitcom.campusmapperNDrawer.outdoordata.Building;



public class RDFReader{
	
    // Set the SPARQL endpoint URI
    private static final String SPARQL_ENDPOINT_URI = "http://data.uni-muenster.de:8080/openrdf-workbench/repositories/indoormapping/query";
	private Bitmap picture;
	
    
//    private QueryExecution mLocalQueryExecution;


    public FloorPlan loadFloorPlanFromSD(FloorPlan floorPlan){
    	
    	
    	 // load model
        Model model = getLocalFloorPlanModel();
        
        if(model == null) {
        	Log.e("local model error", "could not load model");
        	return null;
        }

        
        StmtIterator iter = model.listStatements();
            while (iter.hasNext()) {
                Log.v("model", iter.nextStatement().toString());
            }


    	 floorPlan = getFloorPlanFromSD(model, floorPlan);
    	 
 		getRooms(floorPlan, model);
 		getDoors(floorPlan, model);
 		getEntrances(floorPlan, model);
 		getStairs(floorPlan, model);
 		getElevators(floorPlan, model);
    	 
//        // Important - free up resources used running the query
//		if (mLocalQueryExecution != null) mLocalQueryExecution.close(); 
 		
    	return floorPlan;
    }
    
    public boolean localDataAvailable(){
      	 // get file
     	File sdcard = Environment.getExternalStorageDirectory();
     	File file = new File(sdcard,"CampusMapper/temp.ttl");
     	
     	return file.exists();
    	
    }
    
    private FloorPlan getFloorPlanFromSD(Model model, FloorPlan floorPlan){
    	
   	 String queryString = "SELECT DISTINCT ?floor ?escapePlan ?source ?cropped ?id WHERE {"
 			+ "?floor a <http://vocab.lodum.de/limap/floor> ."
 				+ "?floor <http://vocab.lodum.de/limap/hasEscapePlan> ?escapePlan ."
 				+ "?escapePlan <http://vocab.lodum.de/limap/hasSourceImage> ?source ."
 				+ "?escapePlan <http://vocab.lodum.de/limap/hasCroppedImage> ?cropped ."
 				+ "?escapePlan <http://vocab.lodum.de/limap/id> ?id ."
 				+ "}";
    	  
		QueryExecution queryExecution = null;
		if (model != null)
			queryExecution = QueryExecutionFactory.create(queryString, model);
		ResultSet resultSet = query(queryString, queryExecution);
		if (resultSet == null)
			return null;
     	
     
     // Iterate through all resulting rows
     while (resultSet.hasNext()) {
     	
     	QuerySolution solution = resultSet.next();
     	
     	Log.v("solution", solution.toString());
     	
     	if (solution.get("escapePlan") != null) {

     		String floorURI = solution.getResource("floor").getURI();
     		String escapePlan = solution.getResource("escapePlan").getURI();
     		String source = solution.getLiteral("source").getLexicalForm();
     		String cropped = solution.getLiteral("cropped").getLexicalForm();
     		String id = solution.getLiteral("id").getLexicalForm();
     				
     		floorPlan.setFloorURI(floorURI);
     		floorPlan.setEscapePlanURI(escapePlan);
     		floorPlan.setSourceFloorPlanImageUri(Uri.parse(source));
     		floorPlan.setCroppedFloorPlanImageUri(Uri.parse(cropped));
     		floorPlan.setId(id);
     	
     		return floorPlan;
     	}
     }
     if(queryExecution != null ) queryExecution.close();
     
  	Log.e("model to floorplan error", "could not parse floorPlan from model");
   	 return floorPlan;
    }
    
    private Model getLocalFloorPlanModel(){
    	
   	 // create model
   	 Model model = ModelFactory.createDefaultModel();

   	 // get file
 	File sdcard = Environment.getExternalStorageDirectory();
 	File file = new File(sdcard,"CampusMapper/temp.ttl");
   	 
   	 // get Input stream
 	InputStream in = null;
   	 

    	try {
    		in = FileManager.get().open(file.getAbsolutePath());
         	 model.read(in, "N-TRIPLE");
    		in.close();
    	}
    	catch (IOException e) {
    		e.printStackTrace();
    		return null;
    	}
    	
    	
    	return model;
    }

    
	
	/*
	 * returns list of all available floorplans in particular floor in building defined by URI
	 */
	public ArrayList<FloorPlan> getFloorPlanList(String buildingURI, String floorNumber, String buildingName){
		
		// getEscapePlans with imageURIs and Ids
		String queryString = "SELECT DISTINCT ?floor ?escapePlan ?source ?cropped ?id WHERE {"
				+"?floor a <http://vocab.lodum.de/limap/floor> ;"
				+"<http://vocab.lodum.de/limap/isFloorIn> <" + buildingURI +"> ;"
				+"<http://vocab.lodum.de/limap/hasFloorNumber> \"" + floorNumber + "\" ."
				+"?floor <http://vocab.lodum.de/limap/hasEscapePlan> ?escapePlan ."
				+"?escapePlan <http://vocab.lodum.de/limap/hasSourceImage> ?source ."
				+"?escapePlan <http://vocab.lodum.de/limap/hasCroppedImage> ?cropped ."
				+"?escapePlan <http://vocab.lodum.de/limap/id> ?id ."
				+"} ORDER BY ?id";
        ResultSet resultSet = query(queryString, null);
        if (resultSet == null) return null;
        
        // Setup a place to house results for output
		ArrayList<FloorPlan> floorPlans = new ArrayList<FloorPlan>();

        // Iterate through all resulting rows
        while (resultSet.hasNext()) {
        	
        	QuerySolution solution = resultSet.next();
        	
        	Log.v("solution", solution.toString());
        	
        	if (solution.get("escapePlan") != null) {

        		String floorURI = solution.getResource("floor").getURI();
        		String escapePlan = solution.getResource("escapePlan").getURI();
        		String source = solution.getLiteral("source").getLexicalForm();
        		String cropped = solution.getLiteral("cropped").getLexicalForm();
        		String id = solution.getLiteral("id").getLexicalForm();
        				
        		FloorPlan floorPlan = new FloorPlan();
        		floorPlan.setFloorURI(floorURI);
        		floorPlan.setEscapePlanURI(escapePlan);
        		floorPlan.setSourceFloorPlanImageUri(Uri.parse(source));
        		floorPlan.setCroppedFloorPlanImageUri(Uri.parse(cropped));
        		floorPlan.setId(id);
        		floorPlan.setBuildingURI(buildingURI);
        		floorPlan.setFloorNumber(Integer.parseInt(floorNumber));
        		floorPlan.setBuildingName(buildingName);
        	
        		floorPlans.add(floorPlan);
        	}
        }
		
		return floorPlans;
	}
	
	
	/*
	 * returns all data available for one particular floorplan
	 */
	public void getFloorPlan (FloorPlan floorPlan){

		
		/*
		 *  getRooms (including corridors)
		 *  
		 *  getLocalCoordinates
		 *  can be room or corridor
		 *  
		 *  getPersons
		 *  getNames (of Persons)
		 */
		getRooms(floorPlan, null);
		
		/*
		 *  getDoors
		 *  
		 *  getLocalCoordinates
		 */
		getDoors(floorPlan, null);
		
		/*
		 *  getEntrances
		 *  
		 *  getLocalCoordinates
		 *  If more than one, check which is in which plan
		 *  
		 *  getGlobalCoordinates
		 */
		getEntrances(floorPlan, null);
		
		/*
		 *  getStairs
		 *  
		 *  getLocalCoordinates
		 *  If more than one, check which is in which plan
		 */
		getStairs(floorPlan, null);
		
		/*
		 *  getElevators
		 *  
		 *  getLocalCoordinates
		 *  If more than one, check which is in which plan
		 */
		getElevators(floorPlan, null);
		
	}
	
	



	private FloorPlan getRooms(FloorPlan floorPlan, Model model){
		
		String queryString = "SELECT DISTINCT ?room WHERE {"
				+"<" + floorPlan.getFloorURI()+"> <http://vocab.lodum.de/limap/hasRoom> ?room ;"
				+"}";
		
		QueryExecution queryExecution = null;
		if(model != null)
		queryExecution = QueryExecutionFactory.create(queryString, model);
        ResultSet resultSet = query(queryString, queryExecution);
        
        ArrayList<Room> rooms = new ArrayList<Room>();
        ArrayList<Corridor> corridors = new ArrayList<Corridor>();
        
        
        // Iterate through all resulting rows
        while (resultSet.hasNext()) {
        	
        	QuerySolution solution = resultSet.next();
        	
        	Log.v("solution rooms", solution.toString());
        	
        	if (solution.get("room") != null) {

        		String roomURI = solution.getResource("room").getURI();
        		ArrayList<Geometry> geometries = getLocalCoordinates(roomURI, model);
        		
        		if (geometries.size() == 1 && geometries.get(0).getClass() == Point.class){
        			// should be a room than
        			
        			// get room name
        			String roomName = getRoomName(roomURI, model);
        			
        			// get persons and add new room
        			rooms.add(new Room(geometries.get(0), roomName, getPersons(roomURI, model)));
        			
        		} else {
        			// should be a corridor than
        			ArrayList<Line> lines = new ArrayList<Line>();
        			for(Geometry g : geometries){
        				lines.add((Line) g);
        			}
        			MultiLine ml = new MultiLine(lines);
        			corridors.add(new Corridor(ml));
        		}
        		
        		
        	}
        }
        if(queryExecution != null ) queryExecution.close();
        
		floorPlan.setRooms(rooms);
		floorPlan.setCorridors(corridors);
		return floorPlan;
	}

	private ArrayList<Person> getPersons(String roomURI, Model model){
		String queryString = "SELECT DISTINCT ?user ?name WHERE {"
				+"<" + roomURI+"> <http://vocab.lodum.de/limap/user> ?user ."
				+"?user <http://xmlns.com/foaf/0.1/name> ?name ."
				+"}";
		
		QueryExecution queryExecution = null;
		if(model != null)
		queryExecution = QueryExecutionFactory.create(queryString, model);
        ResultSet resultSet = query(queryString, queryExecution);

        ArrayList<Person> persons = new ArrayList<Person>();
        
        // Iterate through all resulting rows
        while (resultSet.hasNext()) {
        	
        	QuerySolution solution = resultSet.next();	
        	Log.v("solution person", solution.toString());
        	
        	if (solution.get("name") != null) {

        		Person person = new Person(solution.getLiteral("name").getLexicalForm());
        		person.setUri(solution.getResource("user").getURI());
        		persons.add(person);
        	}
        }
        if(queryExecution != null ) queryExecution.close();

        
        return persons;
	}
	
	private String getRoomName(String roomURI, Model model){
		String queryString = "SELECT DISTINCT ?name WHERE {"
				+"<" + roomURI+"> <http://xmlns.com/foaf/0.1/name> ?name ;"
				+"}";
		
		QueryExecution queryExecution = null;
		if(model != null)
		queryExecution = QueryExecutionFactory.create(queryString, model);
        ResultSet resultSet = query(queryString, queryExecution);

        // Iterate through all resulting rows
        while (resultSet.hasNext()) {
        	
        	QuerySolution solution = resultSet.next();	
        	Log.v("solution room name", solution.toString());
        	
        	if (solution.get("name") != null) {

        		return solution.getLiteral("name").getLexicalForm();
        	}
        }
        if(queryExecution != null ) queryExecution.close();

        
        return null;
	}
	

	
	
	
	
	
	private ArrayList<Geometry> getLocalCoordinates(String uriString, Model model){
		ArrayList<Geometry> geometries = new ArrayList<Geometry> ();

		String queryString = "SELECT DISTINCT ?geo ?escapePlan ?floorNumber WHERE { "
				+"<" + uriString +"> <http://vocab.lodum.de/limap/hasLocalCoordinates> ?localCoordinates ."
				+"?localCoordinates <http://www.opengis.net/ont/OGC-GeoSPARQL/1.0/hasGeometry> ?geo ."
				+"?localCoordinates <http://vocab.lodum.de/limap/inEscapePlan> ?escapePlan ."
				+"?floor <http://vocab.lodum.de/limap/hasEscapePlan> ?escapePlan ."
				+"?floor <http://vocab.lodum.de/limap/hasFloorNumber> ?floorNumber ."
				+"}";
		
		QueryExecution queryExecution = null;
		if(model != null)
		queryExecution = QueryExecutionFactory.create(queryString, model);
        ResultSet resultSet = query(queryString, queryExecution);
        
        // Iterate through all resulting rows
        while (resultSet.hasNext()) {
        	
        	QuerySolution solution = resultSet.next();
        	
        	Log.v("solution local coordinates", solution.toString());
        	
        	if (solution.get("geo") != null) {
        		/*
        		 * either this form: "Line((320 281, 353 108))"
        		 * or that: "Point((349 407))"
        		 */
        		String geoString = solution.getLiteral("geo").getLexicalForm();
        		String escapePlanURI = solution.getResource("escapePlan").getURI();
        		String floorNumber = solution.getLiteral("floorNumber").getLexicalForm();

        		// parse geo string
        		Geometry geo = stringToGeometry(geoString);
        		geo.setEscapePlanURI(escapePlanURI);
        		FloorPlan fp = new FloorPlan();
        		fp.setFloorNumber(Integer.parseInt(floorNumber));
        		geo.setFloorPlan(fp);
        		geometries.add(geo);
        	}
        }
        if(queryExecution != null ) queryExecution.close();
		
		
		return geometries;
	}
	
	private GeoPoint getGlobalCoordinates(String uriString, Model model){

		String queryString = "SELECT DISTINCT ?geo WHERE {"
				+"<" + uriString +"> <http://vocab.lodum.de/limap/hasGlobalCoordinates> ?globalCoordinates ."
				+"?globalCoordinates <http://www.opengis.net/ont/OGC-GeoSPARQL/1.0/hasGeometry> ?geo ."
				+"}";
		
		QueryExecution queryExecution = null;
		if(model != null)
		queryExecution = QueryExecutionFactory.create(queryString, model);
        ResultSet resultSet = query(queryString, queryExecution);
        
        // Iterate through all resulting rows
        while (resultSet.hasNext()) {
        	
        	QuerySolution solution = resultSet.next();
        	
        	Log.v("solution local coordinates", solution.toString());
        	
        	if (solution.get("geo") != null) {
        		/*
        		 * form: "Point((34.232 4.232323))"
        		 */
        		String geoString = solution.getLiteral("geo").getLexicalForm();

        		// parse geo string
        		return stringToGeoPoint(geoString);
        	}
        }
        if(queryExecution != null ) queryExecution.close();
		
		return null;
	}

	private GeoPoint stringToGeoPoint(String geoString){
		
		GeoPoint geo = null;
	
        // find last appearance of "("
		int start = geoString.lastIndexOf("(") + 1;
        // find first appearance of ")"
		int end = geoString.indexOf(")");
		
		// create substring
		// in between we have our coordinates
		geoString = geoString.substring(start, end);
		
		
			// it is a point than
			// split the pair at the blank to get x respectively y
			try {
				String [] latLong = geoString.split(" ");
				geo = new GeoPoint(Double.parseDouble(latLong[1]), Double.parseDouble(latLong[0]));		
			} catch (NumberFormatException e) {
				Log.e("debug", "error while parsing Point.");
			}
			
		return  geo;
	}
	
	private Geometry stringToGeometry(String geoString){
		
		Geometry geo = null;
		
        // find last appearance of "("
		int start = geoString.lastIndexOf("(") + 1;
        // find first appearance of ")"
		int end = geoString.indexOf(")");
		
		// create substring
		// in between we have our coordinates
		geoString = geoString.substring(start, end);
		
		// split where we have ", " to get pairs of lat and long
		String [] xyPairs = geoString.split(", ");
		
		if(xyPairs.length == 1){
			// it is a point than
			// split the pair at the blank to get x respectively y
			try {
				String [] xAndY1 = xyPairs[0].split(" ");
				geo = new Point(Integer.parseInt(xAndY1[0]), Integer.parseInt(xAndY1[1]));		
			} catch (NumberFormatException e) {
				Log.e("debug", "error while parsing Point.");
			}
		} else {
			// it is a line than
			// split the pairs at the blank to get x respectively y
				try {
					String [] xAndY1 = xyPairs[0].split(" ");
					String [] xAndY2 = xyPairs[1].split(" ");
					geo = new Line(new Point(Integer.parseInt(xAndY1[0]), Integer.parseInt(xAndY1[1])), 
							new Point(Integer.parseInt(xAndY2[0]), Integer.parseInt(xAndY2[1])));		
				} catch (NumberFormatException e) {
					Log.e("debug", "error while parsing Line.");
				}
		}
			
		return  geo;
	}
	

	private void getElevators(FloorPlan floorPlan, Model model) {

        /*
         * elevator with local coordinates in the appropriate escape plan...
         * 
         * same goes for the other conections
         */
		String queryString = "SELECT DISTINCT ?elevator WHERE {"
		        + "?elevator a <http://vocab.lodum.de/limap/elevator> ."
		        + "?elevator <http://vocab.lodum.de/limap/hasLocalCoordinates> ?locCoords ."
		        + "?locCoords <http://vocab.lodum.de/limap/inEscapePlan> <" + floorPlan.getEscapePlanURI() + "> ."
		        + "}";
		
		QueryExecution queryExecution = null;
		if(model != null)
		queryExecution = QueryExecutionFactory.create(queryString, model);
        ResultSet resultSet = query(queryString, queryExecution);
        
        // Iterate through all resulting rows
        ArrayList<Elevator> elevators = new ArrayList<Elevator>();
        while (resultSet.hasNext()) {
        	
        	QuerySolution solution = resultSet.next();	
        	Log.v("solution elevator", solution.toString());
        	
        	if (solution.get("elevator") != null) {
        		String elevatorURI = solution.getResource("elevator").getURI();
        		
        		
        		// get the local coordinates
        		ArrayList<Geometry> geometries = getLocalCoordinates(elevatorURI, model);
        		
        		Geometry geometryA = null;
        		ArrayList<Geometry> destinations = new ArrayList<Geometry>();
        		
        		for(Geometry g : geometries){
        			if (g.getEscapePlanURI().equals(floorPlan.getEscapePlanURI())){
        				geometryA = g;
        			} else {
        				destinations.add(g);
        			}
        		}
        		
        		elevators.add(new Elevator(geometryA, destinations));
        	}
        }
        if(queryExecution != null ) queryExecution.close();
        
		floorPlan.setElevators(elevators);
	}


	private void getStairs(FloorPlan floorPlan, Model model) {

		String queryString = "SELECT DISTINCT ?stairs WHERE {"
		        + "?stairs a <http://vocab.lodum.de/limap/stairs> ."
		        + "?stairs <http://vocab.lodum.de/limap/hasLocalCoordinates> ?locCoords ."
		        + "?locCoords <http://vocab.lodum.de/limap/inEscapePlan> <" + floorPlan.getEscapePlanURI() + "> ."
		        + "}";
		
		QueryExecution queryExecution = null;
		if(model != null)
		queryExecution = QueryExecutionFactory.create(queryString, model);
        ResultSet resultSet = query(queryString, queryExecution);
        
        // Iterate through all resulting rows
        ArrayList<Stairs> stairs = new ArrayList<Stairs>();
        while (resultSet.hasNext()) {
        	
        	QuerySolution solution = resultSet.next();	
        	Log.v("solution stairs", solution.toString());
        	
        	if (solution.get("stairs") != null) {
        		String stairsURI = solution.getResource("stairs").getURI();
        		
        		
        		// get the local coordinates
        		ArrayList<Geometry> geometries = getLocalCoordinates(stairsURI, model);
        		
        		Geometry geometryA = null;
        		ArrayList<Geometry> destinations = new ArrayList<Geometry>();

        		
        		for(Geometry g : geometries){
        			if (g.getEscapePlanURI().equals(floorPlan.getEscapePlanURI())){
        				geometryA = g;
        			} else {
        				destinations.add(g);
        			}
        		}
        		
        		stairs.add(new Stairs(geometryA, destinations));
        	}
        }
        if(queryExecution != null ) queryExecution.close();
        
        floorPlan.setStairs(stairs);
	}


	private void getEntrances(FloorPlan floorPlan, Model model) {
		
		String queryString = "SELECT DISTINCT ?entrance WHERE {"
		        + "?entrance a <http://vocab.lodum.de/limap/entrance> ."
		        + "?entrance <http://vocab.lodum.de/limap/hasLocalCoordinates> ?locCoords ."
		        + "?locCoords <http://vocab.lodum.de/limap/inEscapePlan> <" + floorPlan.getEscapePlanURI() + "> ."
		        + "}";
		
		QueryExecution queryExecution = null;
		if(model != null)
		queryExecution = QueryExecutionFactory.create(queryString, model);
        ResultSet resultSet = query(queryString, queryExecution);
        
        // Iterate through all resulting rows
        ArrayList<Entrance> entrances = new ArrayList<Entrance>();
        while (resultSet.hasNext()) {
        	
        	QuerySolution solution = resultSet.next();	
        	Log.v("solution entrance", solution.toString());
        	
        	if (solution.get("entrance") != null) {
        		String entranceURI = solution.getResource("entrance").getURI();
        		
        		
        		// get the local coordinates
        		ArrayList<Geometry> geometries = getLocalCoordinates(entranceURI, model);
        		
        		FloorPlan floorPlanB = null;
        		Geometry geometryA = null;
        		Geometry geometryB = null;
        		
        		for(Geometry g : geometries){
        			if (g.getEscapePlanURI().equals(floorPlan.getEscapePlanURI())){
        				geometryA = g;
        			} else {
        				geometryB = g;
        				floorPlanB = new FloorPlan();
        				floorPlanB.setEscapePlanURI(g.getEscapePlanURI());
        			}
        		}
        		
        		if(geometryB != null){
            		entrances.add(new EntranceIndoor(geometryA, geometryB, floorPlanB));        			
        		} else {

                	// get global coordinates
        			GeoPoint globalCoordinates = getGlobalCoordinates(entranceURI, model);
        			if (globalCoordinates != null){
                		entrances.add(new EntranceOutdoor(geometryA, globalCoordinates));
        			}
        			
        		}        	
        	}
        }
        if(queryExecution != null ) queryExecution.close();
        
        floorPlan.setEntrances(entrances);
	}


	private void getDoors(FloorPlan floorPlan, Model model) {
		
		String queryString = "SELECT DISTINCT ?door WHERE {"
		        + "?door a <http://vocab.lodum.de/limap/door> ."
		        + "?door <http://vocab.lodum.de/limap/hasLocalCoordinates> ?locCoords ."
		        + "?locCoords <http://vocab.lodum.de/limap/inEscapePlan> <" + floorPlan.getEscapePlanURI() + "> ."
		        + "}";
		
		QueryExecution queryExecution = null;
		if(model != null)
		queryExecution = QueryExecutionFactory.create(queryString, model);
        ResultSet resultSet = query(queryString, queryExecution);
        
		ArrayList<Door> doors = new ArrayList<Door>();

		// Iterate through all resulting rows
		while (resultSet.hasNext()) {

			QuerySolution solution = resultSet.next();

			Log.v("solution doors", solution.toString());

			if (solution.get("door") != null) {

				String doorURI = solution.getResource("door").getURI();
				ArrayList<Geometry> geometries = getLocalCoordinates(doorURI, model);
				Line l = (Line) geometries.get(0);
				doors.add(new Door(l));

			}
		}
		if(queryExecution != null ) queryExecution.close();
		
		floorPlan.setDoors(doors);
	}
	
	
	/*
	 * returns list of all available floorplans in particular floor in building defined by URI
	 */
	
	
	
	
	private ResultSet query(String queryString, QueryExecution queryExecution){
		if (queryExecution != null) return localQuery(queryString, queryExecution);
		else return hTTPQuery(queryString);
	}
	
	public ResultSet hTTPQuery(String queryString){
		
    	// Create a Query instance
        Query query = QueryFactory.create(queryString, Syntax.syntaxARQ);
        Log.v("http query", query.toString());

        // Query uses an external SPARQL endpoint for processing
        QueryEngineHTTP qe = new QueryEngineHTTP(SPARQL_ENDPOINT_URI, query);
        String pw = "!nd00rmapping";
        qe.setBasicAuthentication("indoormapping", pw.toCharArray());
        ResultSet rs = null;
            rs = qe.execSelect();	
	        // Important - free up resources used running the query
	        qe.close();
        return rs;
	}
	
	private ResultSet localQuery(String queryString, QueryExecution queryExecution){

        ResultSet rs = null;

        try {
            // Execute the query and obtain results
            rs = queryExecution.execSelect();
		} catch (Exception e) {

			Log.e("debug", "error while querying model from file");
			e.printStackTrace();

			return null;
		}
        
        return rs;
	}
	
	// get all player from DB 
	public ArrayList<Player> getAllPlayers() 
	{
					ArrayList<Player> playerlist=new ArrayList<Player>();

					String readString=
							"PREFIX prv: <http://purl.org/net/provenance/ns/> "+
			  				"PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "+
			  				"PREFIX foaf: <http://xmlns.com/foaf/0.1/> " +
						     "PREFIX indoor:<http://data.uni-muenster.de/context/indoormapping/>  "+ 
						    	"  SELECT DISTINCT  ?nick ?email ?registrationDate ?totalScore WHERE "+
						    	 " { "+
						    	 " 	GRAPH <http://data.uni-muenster.de/context/indoormapping> "+ 
						    	 " 	{ "+
						    	 " 		?x rdf:type prv:Player . "+
						    	 " 		?x foaf:nick ?nick . "+
						    	 " 		?x foaf:mbox ?email . "+
						    	 " 		?x indoor:hasRegistrationDate ?registrationDate . "+ 
						    	 " 		?x indoor:hasTotalPlayerScore ?totalScore . "+
						    	 " 	} "+
						    	"  } ORDER BY DESC(?totalScore) ";
					
				    ResultSet resultSet = null;
					resultSet = this.hTTPQuery(readString);
					
					  try {
						
						while (resultSet.hasNext())
						{
							QuerySolution solution = resultSet.next();
							Log.v("solution players", solution.toString());
				        	
					    	if (solution.get("nick") != null) {
					  
					    		PlayerAdapter player=new PlayerAdapter();
					    		player.setEmail(solution.getLiteral("email").getString());
					    		player.setUsername(solution.getLiteral("nick").getString());
					    		Score s=new Score();
					    		s.setValue(Integer.parseInt(solution.getLiteral("totalScore").getString()));
					    		player.setScoreTotal(s);
					    		player.setRegistrationDate(solution.getLiteral("registrationDate").getString());
					    		playerlist.add(player);
					    	
					    	}
						}
					} catch (Exception e) {
						return null;
					}
					  Log.v("size",String.valueOf(playerlist.size()));
					return playerlist;
	}
	
	//getPlayer from DB
	public Player getPlayer(String IDPlus) 
	{
				
					String readString=
							"PREFIX prv: <http://purl.org/net/provenance/ns/> "+
			  				"PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "+
			  				"PREFIX foaf: <http://xmlns.com/foaf/0.1/> " +
						     "PREFIX indoor:<http://data.uni-muenster.de/context/indoormapping/>  "+ 
						    	"  SELECT DISTINCT  ?nick ?id ?totalScore ?email ?registrationDate WHERE "+
						    	 " { "+
						    	 " 	GRAPH <http://data.uni-muenster.de/context/indoormapping> "+ 
						    	 " 	{ "+
						    	 " 		?x rdf:type prv:Player . "+
						    	 " 		?x foaf:nick ?nick . "+
						    	 " 		?x indoor:hasIDPlus ?id . "+
						    	 " 		?x foaf:mbox ?email . "+
						    	 " 		?x indoor:hasRegistrationDate ?registrationDate . "+ 
						    	 " 		?x indoor:hasTotalPlayerScore ?totalScore . "+
						    	 " 		FILTER (?id = '"+IDPlus+"') . "+
						    	 " 	} "+
						    	"  } ";
					Log.v("readstring",readString);
					RDFReader read=new RDFReader();
				    ResultSet resultSet = null;
					resultSet = read.hTTPQuery(readString);
					PlayerAdapter player=null;
					  try {
						
						while (resultSet.hasNext())
						{
							QuerySolution solution = resultSet.next();
					    	Log.v("qsolution",solution.toString());
					    	if (solution.get("nick") != null) {
					    	
					    		player=new PlayerAdapter();
					    		player.setEmail(solution.getLiteral("email").getString());
					    		player.setUsername(solution.getLiteral("nick").getString());
					    		player.setIDPlus(solution.getLiteral("id").getString());
					    		Score s=new Score();
					    		s.setValue(Integer.parseInt(solution.getLiteral("totalScore").getString()));
					    		player.setScoreTotal(s);
					    		player.setRegistrationDate(solution.getLiteral("registrationDate").getString());
					    		Log.v("player.setScoreTotal(s);", solution.getLiteral("totalScore").getString());
					    	}
						}
					} catch (Exception e) {
						return null;
					}
					
					return player;
	}
	
	public boolean playerIsInDB(String IDPlus) 
	{
					String readString=
							"PREFIX prv: <http://purl.org/net/provenance/ns/> "+
			  				"PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "+
			  				"PREFIX foaf: <http://xmlns.com/foaf/0.1/> " +
						     "PREFIX indoor:<http://data.uni-muenster.de/context/indoormapping/>  "+ 
						    	"  SELECT * WHERE "+
						    	 " { "+
						    	 " 	GRAPH <http://data.uni-muenster.de/context/indoormapping> "+ 
						    	 " 	{ "+
						    	 " 		?x indoor:hasIDPlus '"+IDPlus+"' . "+
						    	 " 	} "+
						    	"  } ";
					Log.v("readstring",readString);
					RDFReader read=new RDFReader();
				    ResultSet resultSet = null;
					resultSet = read.hTTPQuery(readString);
					  try {
						if (resultSet.hasNext())
						{
							return true;
					    }
						}
					 catch (Exception e) {
						return false;
					}
					
					return false;
	}
	
	

		
		public ArrayList<Ownership<Building>> getOwnersBuildings() {
			
			ArrayList<Ownership<Building>> playerlist=new ArrayList<Ownership<Building>>();
			String readString=
			 "PREFIX prv: <http://purl.org/net/provenance/ns/> "+
			 "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>  prefix db: <http://dbpedia.org/ontology/> "+
			 "PREFIX foaf: <http://xmlns.com/foaf/0.1/>  prefix geo: <http://www.w3.org/2003/01/geo/wgs84_pos#> "+
				"		     PREFIX indoor:<http://data.uni-muenster.de/context/indoormapping/>   "+
				"		    SELECT ?nick ?email ?name ?lat ?long (SUM(?score)) AS ?totalscore)"+
				"	 WHERE "+
				"	{ {"+
				"		    		GRAPH <http://data.uni-muenster.de/context/indoormapping> "+
				"		    	  	{ "+
			
				"				    ?player foaf:nick ?nick ."+ 
				"				    ?player foaf:mbox ?email . "+
				"				    ?player indoor:hasRelationToFloorPlan ?scoreplan ."+ 
				
				"		    	  	?scoreplan rdf:type prv:FloorScoreRelation .	"+
				"		    	   	?scoreplan indoor:hasFloor ?floor . "+
				"					?floor <http://vocab.lodum.de/limap/isFloorIn> ?building ."+
				
				"		    	   	?scoreplan indoor:hasPlayerScore ?score ."+
				"		    	   	?scoreplan indoor:hasScoreDate ?date  ."+
				"}"+
				
				"					?building a db:building . ?building foaf:name ?name . ?building geo:lat ?lat . ?building geo:long ?long ."+
				"}"+
				"				UNION"+
				"				{"+
				"					?building a db:building . ?building foaf:name ?name . ?building geo:lat ?lat . ?building geo:long ?long ."+
				
				"				}} "+
			 	"	 GROUP BY ?nick ?email ?name ?lat ?long ORDER BY  ?name DESC(?totalscore)";
			
		    ResultSet resultSet = null;
		    Log.v("queryyy", readString);
			resultSet = this.hTTPQuery(readString);
			int i=0;
			int j=0;
			int k=0;
			int o=0;
			boolean first=true;
			String previousBuildingName="";
			  try {
				
				while (resultSet.hasNext())
				{
					QuerySolution solution = resultSet.next();
					Log.v("solutionOwnership", solution.toString());
					Log.v("i",String.valueOf(i));
			    	if (first)
			    	{
			    		Ownership<Building> owner=new Ownership<Building>();
			    		
			    		Building t=new Building();
			    		String buildingName=solution.get("name").toString().substring(0, solution.get("name").toString().length()-41);
		    			t.setBuildingName(buildingName);
		    			
		    			GeoPoint point=new GeoPoint(Float.parseFloat(solution.get("lat").toString()), Float.parseFloat(solution.get("long").toString()));
		    			t.setCenter(point);
		    			owner.setT(t);
		    		
			    		if(solution.get("nick")!=null)
			    		{
			    			PlayerAdapter p=new PlayerAdapter();
			    			p.setEmail(solution.get("email").toString());
			    			p.setUsername(solution.get("nick").toString());
			    			String totalscore=solution.get("totalscore").toString().substring(0, solution.get("totalscore").toString().length()-42);
			    			Score s=new Score(Integer.parseInt(totalscore));
			    			owner.setP(p);
			    			owner.setS(s);
			    			
			    		}
			    		first=false;
			    		previousBuildingName=solution.get("name").toString();
			    		playerlist.add(owner);
			    		
			    		
			    	}
			    	else
			    	{i++;
			    		if(!solution.get("name").toString().equals(previousBuildingName))
			    		{
			    			Ownership<Building> owner=new Ownership<Building>();
				    		
				    		Building t=new Building();
				    		String buildingName=solution.get("name").toString().substring(0, solution.get("name").toString().length()-41);
			    			t.setBuildingName(buildingName);
			    			GeoPoint point=new GeoPoint(Float.parseFloat(solution.get("lat").toString()), Float.parseFloat(solution.get("long").toString()));
			    			t.setCenter(point);
			    			owner.setT(t);
			    			
				    		if(solution.get("nick")!=null)
				    		{
				    			PlayerAdapter p=new PlayerAdapter();
				    			p.setEmail(solution.get("email").toString());
				    			p.setUsername(solution.get("nick").toString());
				    			String totalscore=solution.get("totalscore").toString().substring(0, solution.get("totalscore").toString().length()-42);
				    			Score s=new Score(Integer.parseInt(totalscore));
				    			owner.setP(p);
				    			owner.setS(s);
				    		}
				    		previousBuildingName=solution.get("name").toString();
				    		playerlist.add(owner);
			    		}
			    	}
			    	
				}
			} catch (Exception e) {
				 Log.v("playerList.size()", String.valueOf(playerlist.size()));
				return null;
			}
			 
			return playerlist;
		}
	
		
public ArrayList<Ownership<FloorPlan>> getOwnersFloors(String buildingName) {
			
			ArrayList<Ownership<FloorPlan>> playerlist=new ArrayList<Ownership<FloorPlan>>();
			String readString=
			 "PREFIX prv: <http://purl.org/net/provenance/ns/> "+
			 "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>  prefix db: <http://dbpedia.org/ontology/> "+
			 "PREFIX foaf: <http://xmlns.com/foaf/0.1/>  prefix geo: <http://www.w3.org/2003/01/geo/wgs84_pos#> "+
				"		     PREFIX indoor:<http://data.uni-muenster.de/context/indoormapping/>   "+
				"		    SELECT ?nick ?email ?id ?lat ?long ?name ?floor ?number (SUM(?score)) AS ?totalscore) "+
				"	 WHERE "+
				"	{ {"+
				"		    		GRAPH <http://data.uni-muenster.de/context/indoormapping> "+
				"		    	  	{ "+
			
				"				    ?player foaf:nick ?nick ."+ 
				"				    ?player indoor:hasIDPlus ?id ."+ 
				"				    ?player foaf:mbox ?email . "+
				"				    ?player indoor:hasRelationToFloorPlan ?scoreplan ."+ 
				
				"		    	  	?scoreplan rdf:type prv:FloorScoreRelation .	"+
				"		    	   	?scoreplan indoor:hasFloor ?floor . "+
				"					?floor <http://vocab.lodum.de/limap/isFloorIn> ?building ."+
				"					?floor <http://vocab.lodum.de/limap/hasFloorNumber> ?number ."+				
				"		    	   	?scoreplan indoor:hasPlayerScore ?score ."+
				"		    	   	?scoreplan indoor:hasScoreDate ?date . "+
				"}"+
				
				"					?building a db:building . ?building foaf:name ?name . ?building geo:lat ?lat . ?building geo:long ?long . "+
				"}"+
			
				"				FILTER ( ?name= '"+buildingName+"') . } "+
			 	"	 GROUP BY ?nick ?id ?floor ?number ?name ?email ?lat ?long ORDER BY ?floor DESC(?totalscore)";
			
			
		    ResultSet resultSet = null;
		    Log.v("query_getOwnersFloors", readString);
			resultSet = this.hTTPQuery(readString);
			boolean first=true;
			String previousFloorUri="";
			  try {
				
				while (resultSet.hasNext())
				{
					QuerySolution solution = resultSet.next();
					Log.v("solutionOwnershipFloor", solution.toString());
					if(solution.get("floor")!=null)
					{
			    	if (first)
			    	{
			    		Ownership<FloorPlan> owner=new Ownership<FloorPlan>();
			    		
			    		FloorPlan t=new FloorPlan();
		    			t.setFloorURI(solution.get("floor").toString());
		    			String bName=solution.get("name").toString().substring(0, solution.get("name").toString().length()-41);
		    			t.setBuildingName(bName);
		    			t.setFloorNumber(Integer.parseInt(solution.get("number").toString()));
		    			
		    			owner.setT(t);
			    		PlayerAdapter p=new PlayerAdapter();
			    			
			    			p.setEmail(solution.get("email").toString());
			    			p.setIDPlus(solution.get("id").toString());
			    			
			    			p.setUsername(solution.get("nick").toString());
			    			owner.setP(p);
			    			String totalscore=solution.get("totalscore").toString().substring(0, solution.get("totalscore").toString().length()-42);
			    			Score s=new Score(Integer.parseInt(totalscore));
			    			owner.setS(s);
			    		
			    		first=false;
			    		previousFloorUri=solution.get("floor").toString();
			    		playerlist.add(owner);
			    	}
			    	else
			    	{
			    		if(!solution.get("floor").toString().equals(previousFloorUri))
			    		{
			    			Ownership<FloorPlan> owner=new Ownership<FloorPlan>();
				    		
			    			FloorPlan t=new FloorPlan();
			    			t.setFloorURI(solution.get("floor").toString());
			    			String bName=solution.get("name").toString().substring(0, solution.get("name").toString().length()-41);
			    			t.setBuildingName(bName);
			    			t.setFloorNumber(Integer.parseInt(solution.get("number").toString()));
			    			owner.setT(t);
				    		PlayerAdapter p=new PlayerAdapter();
				    			p.setEmail(solution.get("email").toString());
				    			p.setIDPlus(solution.get("id").toString());
				    			p.setUsername(solution.get("nick").toString());
				    			owner.setP(p);
				    			String totalscore=solution.get("totalscore").toString().substring(0, solution.get("totalscore").toString().length()-42);
				    			Score s=new Score(Integer.parseInt(totalscore));
				    			owner.setP(p);
				    			owner.setS(s);
				    		
				    		previousFloorUri=solution.get("floor").toString();
				    		playerlist.add(owner);
			    		}
			    	}
			    	
				}else return null; 
				}
				
			} catch (Exception e) {
				 Log.v("exception","exc");
				return null;
			}
			 
			  Log.v("sizeeee",String.valueOf(playerlist.size()));
			return playerlist;
		}

		/////////////////////////////////////////////////////////my Queryy

public ArrayList<Ownership<Building>> getOwnersBuildings2() {
	
	ArrayList<Ownership<Building>> playerlist=new ArrayList<Ownership<Building>>();
	String readString=
	 "PREFIX prv: <http://purl.org/net/provenance/ns/> "+
	 "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>  prefix db: <http://dbpedia.org/ontology/> "+
	 "PREFIX foaf: <http://xmlns.com/foaf/0.1/>  prefix geo: <http://www.w3.org/2003/01/geo/wgs84_pos#> "+
		"		     PREFIX indoor:<http://data.uni-muenster.de/context/indoormapping/>   "+
		"		    SELECT ?nick ?id ?email ?name ?lat ?long ?building (SUM(?score)) AS ?totalscore)"+
		"	 WHERE "+
		"	{ {"+
		"		    		GRAPH <http://data.uni-muenster.de/context/indoormapping> "+
		"		    	  	{ "+
	
		"				    ?player foaf:nick ?nick ."+ 
		"                   ?player indoor:hasIDPlus ?id ."+
		"				    ?player foaf:mbox ?email . "+
		"				    ?player indoor:hasRelationToFloorPlan ?scoreplan ."+ 
		
		"		    	  	?scoreplan rdf:type prv:FloorScoreRelation .	"+
		"		    	   	?scoreplan indoor:hasFloor ?floor . "+
		"					?floor <http://vocab.lodum.de/limap/isFloorIn> ?building ."+
		
		"		    	   	?scoreplan indoor:hasPlayerScore ?score ."+
		"		    	   	?scoreplan indoor:hasScoreDate ?date  ."+
		"}"+
		
		"					?building a db:building . ?building foaf:name ?name . ?building geo:lat ?lat . ?building geo:long ?long ."+
		"}"+
		"				} "+
	 	"	 GROUP BY ?nick ?id ?email ?name ?lat ?long ?building ORDER BY  ?name DESC(?totalscore)";
	
    ResultSet resultSet = null;
    Log.v("queryyy", readString);
	resultSet = this.hTTPQuery(readString);
	String previousBuildingName="pippo";
	  try {
		
		while (resultSet.hasNext())
		{
			QuerySolution solution = resultSet.next();
			Log.v("solutionOwnership", solution.toString());
	    	if(!solution.get("name").toString().equals(previousBuildingName))
	    		{
	    			Ownership<Building> owner=new Ownership<Building>();
		    		
		    		Building t=new Building();
		    		String buildingName=solution.get("name").toString().substring(0, solution.get("name").toString().length()-41);
	    			t.setBuildingName(buildingName);
	    			t.setBuildingURI(solution.getResource("building").getURI());
         
	    			GeoPoint point=new GeoPoint(Float.parseFloat(solution.get("lat").toString()), Float.parseFloat(solution.get("long").toString()));
	    			t.setCenter(point);
	    			owner.setT(t);
    				PlayerAdapter p=new PlayerAdapter();
	    			p.setEmail(solution.get("email").toString());
	    			p.setUsername(solution.get("nick").toString());
	    			p.setIDPlus(solution.get("id").toString());
	    			String totalscore=solution.get("totalscore").toString().substring(0, solution.get("totalscore").toString().length()-42);
	    			Score s=new Score(Integer.parseInt(totalscore));
	    			owner.setP(p);
	    			owner.setS(s);
		    		previousBuildingName=solution.get("name").toString();
		    		playerlist.add(owner);
	    	}	
		}
	} catch (Exception e) {
		 Log.v("playerList.size()", String.valueOf(playerlist.size()));
		return null;
	}
	 
	return playerlist;
}

//get all player from DB con score temporal and not
		public ArrayList<Player> getAllPlayersScore(String[] daysAgo, String today,String[] area) 
		{
						ArrayList<Player> playerlist=new ArrayList<Player>();
						String select_string="?nick ?email (SUM(?totalScore)) AS ?totalScore) ";
						String tempscores_string="";
						for(int i=0;i<daysAgo.length;i++)
						{
							tempscores_string=tempscores_string+" UNION{ "+ getTemporalScore(daysAgo[i],today,i)+ "} ";
							select_string=select_string+" (SUM(?scoretemp"+i+")) AS ?tempScore"+i+") ";
						}
						String geoscores_string="";
						for(int i=0;i<area.length;i++)
						{
							geoscores_string=geoscores_string+" UNION{ "+ getGeoScore(area[i],i)+ "} ";
							select_string=select_string+" (SUM(?scoregeo"+i+")) AS ?geoScore"+i+") ";
						}
						String totalScore=getTotalScore2();
						
						String readString=
						 "PREFIX prv: <http://purl.org/net/provenance/ns/> "+
						 "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "+
						 "PREFIX foaf: <http://xmlns.com/foaf/0.1/> "+
						 "PREFIX xsd: 	<http://www.w3.org/2001/XMLSchema#> "+
							"		     PREFIX indoor:<http://data.uni-muenster.de/context/indoormapping/>   "+
							"		    SELECT "+select_string+
							"	 WHERE "+
							"	{ "+
							"		    		GRAPH <http://data.uni-muenster.de/context/indoormapping> "+
							"		    	  	{ "+
							"				{			"
							+ totalScore+
						    " 	 	 }"
							+ tempscores_string
							+ geoscores_string
							+ "} "
						 	+"	} GROUP BY ?nick ?email ";
						Log.v("readstring",readString);
					    ResultSet resultSet = null;
						resultSet = this.hTTPQuery(readString);
						Log.v("resultset",resultSet.toString());
						  try {
							
							while (resultSet.hasNext())
							{
								QuerySolution solution = resultSet.next();
								Log.v("solution players", solution.toString());
							
						    	if (solution.get("nick") != null) {
						    		
						    		PlayerAdapter player=new PlayerAdapter();
						    		
						    		player.setUsername(solution.getLiteral("nick").getString());
						    		player.setEmail(solution.getLiteral("email").getString());
						    		
						    		Score s=new Score();
						    		s.setValue(Integer.parseInt(solution.getLiteral("totalScore").getString()));
						    		player.setScoreTotal(s);
						    		
						    		ArrayList<Score> score=new ArrayList<Score>();
						    		
						    		for(int i=0;i<daysAgo.length;i++)
						    		{
						    		s=new Score(Integer.parseInt(solution.getLiteral("tempScore"+i).getString()),LeaderboardController.getInstance().getNumDays()[i]);
						    		score.add(s);
						    		}
						    		player.setTemporalScore(score);
						    		
						    		score=new ArrayList<Score>();
						    		for(int i=0;i<area.length;i++)
						    		{
						    		s=new Score(Integer.parseInt(solution.getLiteral("geoScore"+i).getString()),CampusMapperApplication.getInstance().getArea(i+1));
						    		score.add(s);
						    		}
						    		player.setGeographicalScore(score);
						    		playerlist.add(player);
						    	
						    	}
							}
						} catch (Exception e) {
							Log.v("exc","n");
							return null;
						}
						 
						return playerlist;
		}

		
		//get all player from DB con score temporal and not
				public ArrayList<Player> getAllPlayersScore3(String[] daysAgo, String today,String[] area) 
				{
								ArrayList<Player> playerlist=new ArrayList<Player>();
								String select_string="?nick ?email ?totalScore ";
								String tempscores_string="";
								for(int i=0;i<daysAgo.length;i++)
								{
									tempscores_string=tempscores_string+" UNION{ "+ getTemporalScore(daysAgo[i],today,i)+ "} ";
									select_string=select_string+" (SUM(?scoretemp"+i+")) AS ?tempScore"+i+") ";
								}
								
								String totalScore=getTotalScore2();
								
								String readString=
								 "PREFIX prv: <http://purl.org/net/provenance/ns/> "+
								 "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "+
								 "PREFIX foaf: <http://xmlns.com/foaf/0.1/> "+
								 "PREFIX xsd: 	<http://www.w3.org/2001/XMLSchema#> "+
									"		     PREFIX indoor:<http://data.uni-muenster.de/context/indoormapping/>   "+
									"		    SELECT "+select_string+
									"	 WHERE "+
									"	{ "+
									"		    		GRAPH <http://data.uni-muenster.de/context/indoormapping> "+
									"		    	  	{ "+
									"				{			"
									+ totalScore+
								    " 	 	 }"
									+ tempscores_string
									
									+ "} "
								 	+"	} GROUP BY ?nick ?email ?totalScore";
								Log.v("readstring",readString);
							    ResultSet resultSet = null;
								resultSet = this.hTTPQuery(readString);
								Log.v("resultset",resultSet.toString());
								  try {
									
									while (resultSet.hasNext())
									{
										QuerySolution solution = resultSet.next();
										Log.v("solution players", solution.toString());
									
								    	if (solution.get("nick") != null) {
								    		
								    		PlayerAdapter player=new PlayerAdapter();
								    		
								    		player.setUsername(solution.getLiteral("nick").getString());
								    		player.setEmail(solution.getLiteral("email").getString());
								    		
								    		Score s=new Score();
								    		s.setValue(Integer.parseInt(solution.getLiteral("totalScore").getString()));
								    		player.setScoreTotal(s);
								    		
								    		ArrayList<Score> score=new ArrayList<Score>();
								    		
								    		for(int i=0;i<daysAgo.length;i++)
								    		{
								    		s=new Score(Integer.parseInt(solution.getLiteral("tempScore"+i).getString()),LeaderboardController.getInstance().getNumDays()[i]);
								    		score.add(s);
								    		}
								    		player.setTemporalScore(score);
								    		
								    		score=new ArrayList<Score>();
								    		for(int i=0;i<area.length;i++)
								    		{
								    		s=new Score(Integer.parseInt(solution.getLiteral("geoScore"+i).getString()),CampusMapperApplication.getInstance().getArea(i+1));
								    		score.add(s);
								    		}
								    		player.setGeographicalScore(score);
								    		playerlist.add(player);
								    	
								    	}
									}
								} catch (Exception e) {
									Log.v("exc","n");
									return null;
								}
								 
								return playerlist;
				}


public String getGeoScore(String area,int i)
{
	String queryGeoscore="?player foaf:nick ?nick . "+
						 "?player foaf:mbox ?email . "+
							
							"				?player indoor:hasRelationToFloorPlan ?scoreplan . "+
							"		    	  	?scoreplan rdf:type prv:FloorScoreRelation .	"+
							"		    	   	?scoreplan indoor:hasFloor ?floor . "+
							"		    	   	?scoreplan indoor:hasPlayerScore ?scoregeo"+i+" ."+
							"		    	   	?scoreplan indoor:hasScoreDate ?date  ."+
							"?floor <http://vocab.lodum.de/limap/isFloorIn> ?building ."+
							"FILTER(?building IN ("+area+"))";
	Log.v("queryGeoscore",queryGeoscore);
	return queryGeoscore;

}

public String getTemporalScore(String daysAgo,String today, int i)
{
	String queryTemporalscore="  ?player foaf:nick ?nick ."+ 
			"				?player foaf:mbox ?email ."+ 
			
			"				?player indoor:hasRelationToFloorPlan ?scoreplan ."+ 
			"		    	  	?scoreplan rdf:type prv:FloorScoreRelation .	"+
			"		    	   	?scoreplan indoor:hasFloor ?floor . "+
			"		    	   	?scoreplan indoor:hasPlayerScore ?scoretemp"+i+" ."+
			"		    	   	?scoreplan indoor:hasScoreDate ?date  ."+
			"		    	   	FILTER ( ((xsd:dateTime(?date)) > '"+daysAgo+"'^^xsd:dateTime) && ((xsd:dateTime(?date)) < '"+today+"'^^xsd:dateTime) ) .";
			
	Log.v("queryTemporalscore",queryTemporalscore);
	return queryTemporalscore;

}

public String getTotalScore()
{
	String queryscore="  ?player foaf:nick ?nick ."+ 
			"				?player foaf:mbox ?email ."+ 
			
			"				?player indoor:hasRelationToFloorPlan ?scoreplan ."+ 
			"		    	  	?scoreplan rdf:type prv:FloorScoreRelation .	"+
			"		    	   	?scoreplan indoor:hasFloor ?floor . "+
			"		    	   	?scoreplan indoor:hasPlayerScore ?score ."+
			"		    	   	?scoreplan indoor:hasScoreDate ?date  .";
			
	Log.v("queryscore",queryscore);
	return queryscore;
	}

public String getTotalScore2()
{
	String queryscore="  ?player foaf:nick ?nick ."+ 
			"				?player foaf:mbox ?email ."+ 
			" ?player indoor:hasTotalPlayerScore ?totalScore .";
			
	Log.v("queryscore",queryscore);
	return queryscore;
	}

		
	}

